
export default class BtlMapConverts {
	/**地图节点 */
	protected _rootNode: cc.Node = null;
	/**地图 原点起始的CC位置*/
	protected _zeroOffset: cc.Vec2 = null;
	/** 地图格子数 */
	protected _mapSize: cc.Size = null;
	public get mapSize() {
		return this._mapSize;
	}
	/** 地图节点大小 */
	protected _viewSize: cc.Size = null;
	public get viewSize(): cc.Size {
		return this._viewSize;
	}
	/** 地图单元格子大小 */
	protected _unitSize: cc.Size = null;
	public get unitSize(): cc.Size {
		return this._unitSize;
	}
	/** 地图区域的起始坐标（Map） */
	protected _mapStartPos: cc.Vec2 = null;

	constructor(root: cc.Node, zeroOffset: cc.Vec2, mapSize?: cc.Size, viewSize?: cc.Size, unitSize?: cc.Size, mapStartPos?: cc.Vec2) {
		this._rootNode = root;
		this._zeroOffset = zeroOffset;
		this._mapSize = mapSize;

		let checkParamCount = 0;
		if (!!mapSize) checkParamCount++;
		if (!!viewSize) checkParamCount++;
		if (!!unitSize) checkParamCount++;
		if (checkParamCount < 2) {
			console.error('至少需要其中俩个参数，才能正常进行坐标转换')
		}

		//计算地图相关尺寸
		this._viewSize = viewSize;
		this._unitSize = unitSize;
		if (!this._viewSize) {
			let height = this.unitSize.height * this._mapSize.height;
			let width = this.unitSize.width * this._mapSize.width;
			this._viewSize = cc.size(width, height);
		}
		if (!this._unitSize) {
			let height = this.viewSize.height / mapSize.height
			let width = this.viewSize.width / this._mapSize.width;
			this._unitSize = cc.size(width, height);
		}
		if (!this._mapSize) {
			let width = this.viewSize.width / this.unitSize.width
			let height = this.viewSize.height / unitSize.height;
			this._mapSize = cc.size(width, height);
		}
		this.converToMapPos(cc.v2(0, 0))
		this._mapStartPos = mapStartPos || cc.Vec2.ZERO;
	}

	/** 将地图坐标转换为cocos坐标 */
	public converToCCPos(pos: cc.Vec2 | number, posY?: number): cc.Vec2 {
		let p: cc.Vec2 = null;
		if (pos instanceof cc.Vec2) {
			p = pos;
		}
		else {
			p = cc.v2(pos, posY);
		}
		// 棋盘网格，坐标系转换像素方法
		let x = p.x * this.unitSize.width + 0.5 * this.unitSize.width;
		let y = p.y * this.unitSize.height + 0.5 * this.unitSize.width;
		return cc.v2(x, y);
	}

	/** 将cocos坐标转换为地图坐标 */
	public converToMapPos(pos: cc.Vec2, round: boolean = true): cc.Vec2 {
		pos.x -= this._zeroOffset.x;
		pos.y -= this._zeroOffset.x;
		let x = (pos.x / this.unitSize.width)
		if (round) {
			x = Math.round(x);
		}
		let y = pos.y / this.unitSize.height;
		if (round) {
			y = Math.round(y);
		}
		let position = cc.v2(x, y);
		return position
	}

	/** 将某个节点的坐标转换为地图节点的地图坐标 */
	public converNodePosToMapPos(node: cc.Node, pos: cc.Vec2): cc.Vec2 {
		let worldPos = node.convertToWorldSpaceAR(pos);
		let ccPos = this._rootNode.convertToNodeSpaceAR(worldPos);
		return this.converToMapPos(ccPos);
	}

	/** 将地图某个点的坐标转换到某个节点下的坐标 */
	public converMapPosToNodePos(node: cc.Node, pos: cc.Vec2): cc.Vec2 {
		let ccPos = this.converToCCPos(pos);
		let woldPos = this._rootNode.convertToWorldSpaceAR(ccPos);
		return node.convertToNodeSpaceAR(woldPos);
	}
}

